LinuxCommandLibrary

^

Replace previous command's string

TLDR

Run the previous command replacing string1 with string2

$ ^[string1]^[string2]
copy

Remove string1 from the previous command
$ ^[string1]^
copy

Replace string1 with string2 in the previous command and add string3 to its end
$ ^[string1]^[string2]^[string3]
copy

Replace all occurrences of string1
$ ^[string1]^[string2]^:g&
copy

Print the substituted command without running it
$ ^[string1]^[string2]^:p
copy

SYNOPSIS

^old_string^new_string^

PARAMETERS

old_string
    The substring to be replaced in the previous command.

new_string
    The replacement substring.

DESCRIPTION

The ^ character is not a standalone Linux command. Instead, it serves as a special character with multiple contexts and meanings within the Linux environment, primarily within shell scripting and regular expressions. Its most 'command-like' usage is as a shorthand for history substitution in shells like Bash. When used in this context, it allows for a rapid, one-off replacement of a string in the immediately preceding command line. For instance, if you typed a command with a typo and want to correct it quickly without retyping the whole line, you can use ^old^new^ to substitute 'old' with 'new' in the last command and execute the modified command. This feature is a powerful productivity tool for interactive shell use. Beyond history substitution, ^ is also fundamental in regular expressions to denote the beginning of a line or string, and it acts as the bitwise XOR operator in shell arithmetic expansions and various programming languages.

CAVEATS

The ^ character itself is not a standalone Linux command. Its most 'command-like' usage is within shells like Bash for history substitution, which is a shell feature, not an external utility. This substitution only affects the immediately preceding command. For more complex or global string replacements in history, or for general text manipulation, other methods (e.g., using !!:s/old/new/ with flags, or commands like sed) are required.

REGULAR EXPRESSIONS

In regular expressions (used by commands like grep, sed, awk), ^ serves as an anchor that matches the beginning of a line or string. For example, the pattern ^start would only match lines that begin with the word 'start'.

BITWISE XOR OPERATOR

In shell arithmetic expansions (e.g., ((...))) and various programming languages (like C, Python), ^ represents the bitwise XOR (exclusive OR) operator. This performs a logical XOR operation on corresponding bits of two operands. For example, in bash, echo $((5 ^ 3)) would output 6 (binary 101 XOR 011 = 110).

HISTORY

The concise ^old^new^ history substitution syntax originated in the C shell (csh) and was later adopted by the GNU Bash shell due to its convenience for quick edits of the last executed command. This form is a shorthand for the more general history modifier :s.

SEE ALSO

history(1), bash(1), grep(1), sed(1)

Copied to clipboard